﻿#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<Windows.h>
#include<conio.h>
using namespace std;

#define BOOK_NUM 200
#define READER_NUM 100
#define LEN_BOOK sizeof(Book)
#define LEN_READER sizeof(Reader)

//图书结构体
typedef struct Book
{
	int Num;//图书编号
	char Name[15];//图书名称
	char Auther[15];//图书作者
	char Press[15];//图书出版社
	int Amount;//图书数量
}Book;
//读者结构体
typedef struct Reader
{
	int Num;//读者编号
	char Name[15];//读者姓名
	char Sex[4];//读者性别
	int Max;//读者最大可借阅数量
	int Amount;//读者当前可借阅的数量
	int BookId[100];//读者已经借阅的图书列表
}Reader;

//全局结构体记录所有图书信息和读者信息
Book astBook[BOOK_NUM];
Reader astReader[READER_NUM];

void Menu()
{
	system("cls");//清屏
	cout << "\t\t" << "*********** -图书管理主菜单- ***********" << endl << endl;
	cout << "\t\t" << "***********   1.图书管理     ***********" << endl;
	cout << "\t\t" << "***********   2.读者管理     ***********" << endl;
	cout << "\t\t" << "***********   3.借还书登记   ***********" << endl;
	cout << "\t\t" << "***********   0.退出系统     ***********" << endl << endl;
	cout << "\t\t" << "请选择项目：";
}
void ShowBookMenu()
{
	system("cls");//清屏
	cout << "\t\t" << "*********** -图书管理子菜单- ***********" << endl << endl;
	cout << "\t\t" << "***********   1.显示图书     ***********" << endl;
	cout << "\t\t" << "***********   2.添加图书     ***********" << endl;
	cout << "\t\t" << "***********   3.查找图书     ***********" << endl;
	cout << "\t\t" << "***********   4.删除图书     ***********" << endl;
	cout << "\t\t" << "***********   5.修改图书     ***********" << endl;
	cout << "\t\t" << "***********   0.返回主菜单   ***********" << endl << endl;
	cout << "\t\t" << "请选择项目：";
}
void ShowReaderMenu()
{
	system("cls");//清屏
	cout << "\t\t" << "*********** -读者管理子菜单- ***********" << endl << endl;
	cout << "\t\t" << "***********   1.显示读者     ***********" << endl;
	cout << "\t\t" << "***********   2.添加读者     ***********" << endl;
	cout << "\t\t" << "***********   3.查找读者     ***********" << endl;
	cout << "\t\t" << "***********   4.删除读者     ***********" << endl;
	cout << "\t\t" << "***********   5.修改读者     ***********" << endl;
	cout << "\t\t" << "***********   0.返回主菜单   ***********" << endl << endl;
	cout << "\t\t" << "请选择项目：";
}
void ShowBorrowReturnMenu()
{
	system("cls");//清屏
	cout << "\t\t" << "***********  -借还书子菜单-  ***********" << endl << endl;
	cout << "\t\t" << "***********   1.借书登记     ***********" << endl;
	cout << "\t\t" << "***********   2.还书登记     ***********" << endl;
	cout << "\t\t" << "***********   0.返回主菜单   ***********" << endl << endl;
	cout << "\t\t" << "请选择项目：";
}

//读文件到内存中，再内存中操作
int ReadBookFile(const char* pcMode)
{
	int BookRecond = 0;
	FILE* pfBook;//文件指针
	pfBook = fopen("book.txt", pcMode);
	if (pfBook == NULL)
	{
		return -1;
	}
	while (!feof(pfBook))//读文件末尾
	{
		if (fread(&astBook[BookRecond], LEN_BOOK, 1, pfBook))
		{
			BookRecond++;
		}
	}
	fclose(pfBook);
	return BookRecond;
}

//显示图书信息
void ShowBook()
{
	int BookRecord;//记录条数
	system("cls");
	BookRecord = ReadBookFile("rb");//读文件
	if (BookRecord == -1)
	{
		cout << "\t文件打开失败，请添加图书信息" << endl;

	}
	if (BookRecord == 0)
	{
		cout << "\t文件中没有图书信息" << endl;

	}
	else
	{
		cout << endl << "\t**********************************************************" << endl << endl << endl;
		cout << "\t\t" << "编号" << "\t书名" << "\t作者" << "\t出版社" << "\t库存" << endl;
		for (int i = 0; i < BookRecord; i++)
		{
			cout << "\t\t" << astBook[i].Num << "\t" << astBook[i].Name << "\t" << astBook[i].Auther << "\t" << astBook[i].Press << "\t" << astBook[i].Amount << endl;

		}
		cout << endl << endl << "\t**********************************************************" << endl;

	}
}


//添加图书信息
void AddBook()
{
	FILE* pfBook;
	int BookRecord;
	int	FlagExist;//图书编号存在标记
	char Flag;//记录y/n选项
	system("cls");
	BookRecord = ReadBookFile("ab+");//ab+追加方式打开或新建二进制文件
	if (BookRecord == -1)
	{
		cout << "\t文件打开失败" << endl;
		return;
	}
	else if (BookRecord == 0)
	{
		cout << "\t没有图书信息" << endl;
	}
	else
	{
		ShowBook();//如果图书记录不为0则显示所有图书
	}

	//循环录入图书信息
	cout << "\t是否输入信息(y/n)";
	cin >> Flag;
	cout << endl;
	if (Flag == 'n')
	{
		return;
	}
	pfBook = fopen("book.txt", "ab+");
	if (pfBook == NULL)
	{
		cout << "\t文件打开失败" << endl;
		return;
	}
	while (Flag == 'y')
	{
		//录入数据
		if (BookRecord >= BOOK_NUM)
		{
			cout << "\t录入已满" << endl;
			fclose(pfBook);
			return;
		}
		//图书编号不能重复
		cout << "\t请输入图书编号：";

		do {
			FlagExist = 0;
			cin >> astBook[BookRecord].Num;
			cout << endl;

			for (int i = 0; i < BookRecord; i++)//循环判断图书编号，直到编号不重复为止 
			{
				if (astBook[i].Num == astBook[BookRecord].Num)
				{
					FlagExist = 1;
					cout << "\t该图书编号已经存在，请重新输入编号：";
					break;
				}
			}
		} while (FlagExist == 1);

		cout << "\t请输入图书名称：";
		cin >> astBook[BookRecord].Name;
		cout << endl;

		cout << "\t请输入图书作者：";
		cin >> astBook[BookRecord].Auther;
		cout << endl;

		cout << "\t请输入图书出版社：";
		cin >> astBook[BookRecord].Press;
		cout << endl;

		cout << "\t请输入图书库存量：";
		cin >> astBook[BookRecord].Amount;
		cout << endl;


		//将新增的图书信息写入到文件中
		if (fwrite(&astBook[BookRecord], LEN_BOOK, 1, pfBook) != 1)
		{
			cout << "\t无法保存该信息" << endl;
			return;
		}
		else
		{
			cout << "\t" << astBook[BookRecord].Num << "号图书信息已经保存" << endl;
			BookRecord++;
		}

		cout << endl;
		cout << "\t是否继续输入信息(y/n)：";
		cin >> Flag;
		cout << endl;
	}
	fclose(pfBook);
	if (Flag != 'y' && Flag != 'n')
	{
		cout << "\t输入错误，请退出重新输入" << endl;
	}
	else
	{
		cout << "\t成功添加图书" << endl;
	}

}

//查找图书信息
int SearchBook()
{
	int i;
	int BookRecord;
	int BookNum;//需要查找的ID
	int BookID = 0;//查找到的ID
	system("cls");
	BookRecord = ReadBookFile("rb");
	if (BookRecord == -1)
	{
		cout << "\t文件打开失败" << endl;
		cout << endl << "按任意键返回" << endl;
		_getch();
		return -2;//文件打开失败，返回-2
	}
	else if (BookRecord == 0)
	{
		cout << "\t没有图书记录" << endl;
		cout << endl << "按任意键返回" << endl;
		_getch();
		return -3;//文件打开失败，返回-3
	}

	//进行查找
	cout << "\t请输入图书编号：";
	cin >> BookNum;
	cout << endl;

	for (i = 0; i < BookRecord; i++)
	{
		if (BookNum == astBook[i].Num)
		{
			BookID = i;//找到后返回记录编号
			cout << "\t\t" << "编号" << "\t书名" << "\t作者" << "\t出版社" << "\t库存" << endl;
			cout << "\t\t" << astBook[i].Num << "\t" << astBook[i].Name << "\t" << astBook[i].Auther << "\t" << astBook[i].Press << "\t" << astBook[i].Amount << endl;
			break;
		}
	}
	if (i == BookRecord)
	{
		cout << "\t找不到" << BookNum << "号图书信息";
		BookID = -1;
	}
	return BookID;
}

//删除图书信息
void DeleteBook()
{
	FILE* pfbook;
	int BookID;
	int BookRecord;
	char Flag;

	system("cls");
	BookID = SearchBook();
	cout << endl;
	//文件打开失败
	if (BookID == -1)
	{
		return;
	}
	if (BookID == -3)
	{
		return;
	}

	BookRecord = ReadBookFile("rb");
	cout << "\t已经找到该图书，是否删除(y/n)：";
	cin >> Flag;
	cout << endl;

	int index = astBook[BookID].Num;//删除的信息编号

	//把要删除的记录后的数组全部向前移动一位，然后再写入文件中
	if (Flag == 'n')
	{
		return;
	}
	else if (Flag == 'y')
	{
		for (int i = BookID; i < BookRecord - 1; i++)
		{
			astBook[i] = astBook[i + 1];
		}
		BookRecord--;
	}

	//写入文件
	if (Flag == 'y')
	{
		pfbook = fopen("book.txt", "wb");
		if (pfbook != NULL)
		{
			for (int i = 0; i < BookRecord; i++)
			{
				if (fwrite(&astBook[i], LEN_BOOK, 1, pfbook) != 1)
				{
					cout << "\t无法保存该信息" << endl;
					return;
				}
			}
			fclose(pfbook);
			cout << "\t" << index << "号图书信息已经成功删除" << endl;
		}
	}
	else
	{
		cout << "\t输入错误，请退出重新输入" << endl;
	}
}

//保存到文件中
void SaveBookFile(int BookID)
{
	FILE* pfbook;
	pfbook = fopen("book.txt", "rb+");
	if (pfbook != NULL)
	{
		fseek(pfbook, LEN_BOOK * BookID, SEEK_SET);
		if (fwrite(&astBook[BookID], LEN_BOOK, 1, pfbook) != 1)
		{
			cout << "\t无法保存该信息";
		}
	}
	fclose(pfbook);
}


//修改图书信息
void ModifyBook()
{
	int BookID, BookRecord;
	int FlagExist;//图书编号存在标记
	int item, Num;

	system("cls");
	BookID = SearchBook();
	cout << endl;
	//文件打开失败
	if (BookID == -1)
	{
		return;
	}
	if (BookID == -3)
	{
		return;
	}
	//找到该序号的图书，可以进行修改操作
	BookRecord = ReadBookFile("rb");

	//显示修改选项的子菜单
	cout << endl << "\t1.编号  " << "2.书名  " << "3.作者  " << "4.出版社  " << "5.图书库存量" << endl << endl;
	cout << "\t请输入需要修改的相应信息(输入相应数字)：";
	cin >> item;
	if (item > 5 || item < 1)
	{
		cout << endl << "\t输入数字有误，请退出重新输入";
		return;
	}

	switch (item)
	{
	case 1:
		do {
			FlagExist = 0;
			cout << endl << "\t请输入修改后的图书编号：";
			cin >> Num;
			cout << endl;

			for (int i = 0; i < BookRecord; i++)
			{
				if (Num == astBook[i].Num && i != BookID)//数字和已有编号重复不行，更改的编号原来一样可以
				{
					FlagExist = 1;
					cout << "\t输入错误，该图书编号已经存在，请重新输入" << endl;
					break;
				}
			}
		} while (FlagExist == 1);

		astBook[BookID].Num = Num;
		break;
	case 2:
		cout << endl << "\t请输入修改后的图书名称：";
		cin >> astBook[BookID].Name;
		cout << endl;
		break;
	case 3:
		cout << endl << "\t请输入修改后的图书作者：";
		cin >> astBook[BookID].Auther;
		cout << endl;
		break;
	case 4:
		cout << endl << "\t请输入修改后的图书出版社：";
		cin >> astBook[BookID].Press;
		cout << endl;
		break;
	case 5:
		cout << endl << "\t请输入修改后的图书库存量：";
		cin >> astBook[BookID].Amount;
		cout << endl;
		break;
	}
	//保存到文件中
	SaveBookFile(BookID);
	cout << "\t图书信息修改成功";

}

//书籍管理系统
void MangaeBook()
{
	int item;
	ShowBookMenu();
	cin >> item;
	while (item)
	{
		switch (item)
		{
		case 1:
			ShowBook();//显示图书信息
			break;
		case 2:
			AddBook();//添加图书信息
			break;
		case 3:
			SearchBook();//查找图书信息
			break;
		case 4:
			DeleteBook();//删除图书信息
			break;
		case 5:
			ModifyBook();//修改图书信息
			break;
		default:
			cout << "\t\t" << "输入错误，请重新输入" << endl;
			Sleep(1500);
			break;
		}
		cout << endl << "按任意键返回" << endl;
		_getch();//从控件中获取字符而无需显示
		ShowBookMenu();
		cin >> item;
	}
}


int ReadReaderFile(const char* fopenMode)
{
	int ReaderRecord = 0;
	FILE* pfReader;
	pfReader = fopen("reader.txt", fopenMode);
	if (pfReader == NULL)
	{
		return -1;
	}
	while (!feof(pfReader))//feof读文件的末尾
	{
		if (fread(&astReader[ReaderRecord], LEN_READER, 1, pfReader))
		{
			ReaderRecord++;
		}
	}
	fclose(pfReader);
	return ReaderRecord;
}


//显示读者信息
void ShowReader()
{
	int ReaderRecord;//记录条数
	system("cls");
	ReaderRecord = ReadReaderFile("rb");//读文件
	if (ReaderRecord == -1)
	{
		cout << "\t文件打开失败，请添加读者信息" << endl;

	}
	if (ReaderRecord == 0)
	{
		cout << "\t文件中没有读者信息" << endl;

	}
	else
	{
		cout << endl << "\t*******************************************************************" << endl << endl << endl;
		cout << "\t\t" << "编号" << "\t姓名" << "\t性别" << "\t最大可借阅量" << "\t当前可借阅量" << endl;
		for (int i = 0; i < ReaderRecord; i++)
		{
			cout << "\t\t" << astReader[i].Num << "\t" << astReader[i].Name << "\t" << astReader[i].Sex << "\t" << astReader[i].Max << "\t\t" << astReader[i].Amount << endl;

		}
		cout << endl << endl << "\t*******************************************************************" << endl;

	}
}

//添加读者信息
void AddReader()
{
	FILE* pfReader;
	int ReaderRecord;
	int	FlagExist;//读者编号存在标记
	char Flag;//记录y/n选项

	system("cls");
	ReaderRecord = ReadReaderFile("ab+");//ab+追加方式打开或新建二进制文件
	if (ReaderRecord == -1)
	{
		cout << "\t文件打开失败" << endl;
		return;
	}
	else if (ReaderRecord == 0)
	{
		cout << "\t没有读者信息" << endl;
	}
	else
	{
		ShowReader();//如果图书记录不为0则显示所有读者信息
	}

	//循环录入图书信息
	cout << "\t是否输入信息(y/n)";
	cin >> Flag;
	cout << endl;

	if (Flag == 'n')
	{
		return;
	}
	pfReader = fopen("reader.txt", "ab+");
	if (pfReader == NULL)
	{
		cout << "\t文件打开失败" << endl;
		return;
	}
	while (Flag == 'y')
	{
		//录入数据
		if (ReaderRecord >= READER_NUM)
		{
			cout << "\t录入已满" << endl;
			fclose(pfReader);
			return;
		}
		//图书编号不能重复
		cout << "\t请输入读者编号：";

		do {
			FlagExist = 0;
			cin >> astReader[ReaderRecord].Num;
			cout << endl;

			for (int i = 0; i < ReaderRecord; i++)//循环判断图书编号，直到编号不重复为止 
			{
				if (astReader[i].Num == astReader[ReaderRecord].Num)
				{
					FlagExist = 1;
					cout << "\t该读者编号已经存在，请重新输入编号：";
					break;
				}
			}
		} while (FlagExist == 1);

		cout << "\t请输入读者姓名：";
		cin >> astReader[ReaderRecord].Name;
		cout << endl;

		cout << "\t请输入读者性别(F/M)：";
		cin >> astReader[ReaderRecord].Sex;
		cout << endl;
		while (astReader[ReaderRecord].Sex != 0)
		{

			if (strcmp(astReader[ReaderRecord].Sex, "F") == 0 || strcmp(astReader[ReaderRecord].Sex, "M") == 0)
			{
				break;
			}
			cout << "\t输入错误，只许输入'F'或'M',请重新输入";
			cin >> astReader[ReaderRecord].Sex;
			cout << endl;
		}

		cout << "\t请输入读者最大可借阅数量：";//todo可细化判断数量
		cin >> astReader[ReaderRecord].Max;
		cout << endl;

		cout << "\t请输入读者当前可借阅数量：";//两种一开始是相等的
		cin >> astReader[ReaderRecord].Amount;
		cout << endl;


		//将新增的读者信息写入到文件中
		if (fwrite(&astReader[ReaderRecord], LEN_READER, 1, pfReader) != 1)
		{
			cout << "\t无法保存该信息" << endl;
			return;
		}
		else
		{
			cout << "\t" << astReader[ReaderRecord].Num << "号读者信息已经保存" << endl;
			ReaderRecord++;
		}

		cout << endl;
		cout << "\t是否继续输入信息(y/n)：";
		cin >> Flag;
		cout << endl;
	}
	fclose(pfReader);
	if (Flag != 'y' && Flag != 'n')
	{
		cout << "\t输入错误，请退出重新输入" << endl;
	}
	else
	{
		cout << "\t成功添加读者信息" << endl;
	}
}

//查找图读者信息
int SearchReader()
{
	int i;
	int ReaderRecord;
	int ReaderNum;//需要查找的ID
	int ReaderID = 0;//查找到的ID

	system("cls");
	ReaderRecord = ReadReaderFile("rb");
	if (ReaderRecord == -1)
	{
		cout << "\t文件打开失败" << endl;
		cout << endl << "按任意键返回" << endl;
		_getch();
		return -2;//文件打开失败，返回-2
	}
	else if (ReaderRecord == 0)
	{
		cout << "\t没有读者记录" << endl;
		cout << endl << "按任意键返回" << endl;
		_getch();
		return -3;//文件打开失败，返回-3
	}

	//进行查找
	cout << "\t请输入读者编号：";
	cin >> ReaderNum;
	cout << endl;

	for (i = 0; i < ReaderRecord; i++)
	{
		if (ReaderNum == astReader[i].Num)
		{
			ReaderID = i;//找到后返回记录编号
			cout << "\t\t" << "编号" << "\t姓名" << "\t性别" << "\t最大可借阅数量" << "\t当前可借阅的数量" << endl;
			cout << "\t\t" << astReader[i].Num << "\t" << astReader[i].Name << "\t" << astReader[i].Sex << "\t" << astReader[i].Max << "\t\t" << astReader[i].Amount << endl;
			break;
		}
	}
	if (i == ReaderRecord)
	{
		cout << "\t找不到" << ReaderNum << "号读者信息";
		ReaderID = -1;
	}
	return ReaderID;
}

//删除读者信息
void DeleteReader()
{
	FILE* pfReader;
	int ReaderID;
	int ReaderRecord;
	char Flag;

	system("cls");
	ReaderID = SearchReader();
	cout << endl;
	//文件打开失败
	if (ReaderID == -1)
	{
		return;
	}
	if (ReaderID == -3)
	{
		return;
	}

	ReaderRecord = ReadReaderFile("rb");
	cout << "\t已经找到该读者，是否删除(y/n)：";
	cin >> Flag;
	cout << endl;

	int index = astReader[ReaderID].Num;//删除的信息编号

	//把要删除的记录后的数组全部向前移动一位，然后再写入文件中
	if (Flag == 'n')
	{
		return;
	}
	else if (Flag == 'y')
	{
		for (int i = ReaderID; i < ReaderRecord - 1; i++)
		{
			astReader[i] = astReader[i + 1];
		}
		ReaderRecord--;
	}

	//写入文件
	if (Flag == 'y')
	{
		pfReader = fopen("reader.txt", "wb");
		if (pfReader != NULL)
		{
			for (int i = 0; i < ReaderRecord; i++)
			{
				if (fwrite(&astReader[i], LEN_READER, 1, pfReader) != 1)
				{
					cout << "\t无法保存该信息" << endl;
					return;
				}
			}
			fclose(pfReader);
			cout << "\t" << index << "号图书信息已经成功删除" << endl;
		}
	}
	else
	{
		cout << "\t输入错误，请退出重新输入" << endl;
	}
}


//保存到文件中
void SaveReaderFile(int ReaderID)
{
	FILE* pfReader;
	pfReader = fopen("reader.txt", "rb+");
	if (pfReader != NULL)
	{
		fseek(pfReader, LEN_READER * ReaderID, SEEK_SET);
		if (fwrite(&astReader[ReaderID], LEN_READER, 1, pfReader) != 1)
		{
			cout << "\t无法保存该信息";
		}
	}
	fclose(pfReader);
}



//修改读者信息
void ModifyReader()
{
	int ReaderID, ReaderRecord;
	int item, Num, Max;
	int FlagExist;
	int Borrow;//已经借阅的书数目

	system("cls");
	ReaderID = SearchReader();
	cout << endl;
	if (ReaderID == -1)
	{
		return;
	}
	if (ReaderID == -3)
	{
		return;
	}

	//找到该序号读者后进行修改
	ReaderRecord = ReadReaderFile("rb");

	//显示修改选项的子菜单
	cout << endl << "\t\t" << "1.编号" << "\t2.姓名" << "\t3.性别" << "\t4.最大可借阅数量" << endl;
	cout << endl << "\t请输入需要修改的相应信息(输入相应数字)：";
	cin >> item;
	if (item > 4 || item < 1)
	{
		cout << endl << "\t输入数字有误，请退出重新输入";
		return;
	}

	switch (item)
	{
	case 1:
		do {
			FlagExist = 0;
			cout << endl << "\t请输入修改后的读者编号：";
			cin >> Num;
			cout << endl;

			for (int i = 0; i < ReaderRecord; i++)
			{
				if (Num == astReader[i].Num && i != ReaderID)//数字和已有编号重复不行，更改的编号原来一样可以
				{
					FlagExist = 1;
					cout << "\t输入错误，该读者编号已经存在，请重新输入" << endl;
					break;
				}
			}
		} while (FlagExist == 1);

		astReader[ReaderID].Num = Num;
		break;
	case 2:
		cout << endl << "\t请输入修改后的读者名称：";
		cin >> astReader[ReaderID].Name;
		cout << endl;
		break;
	case 3:
		cout << endl << "\t请输入修改后的读者性别(F/M)：";
		cin >> astReader[ReaderID].Sex;
		cout << endl;
		while (astReader[ReaderID].Sex != 0)
		{
			if (strcmp(astReader[ReaderID].Sex, "F") == 0 || strcmp(astReader[ReaderID].Sex, "M") == 0)
			{
				break;
			}
			cout << "\t输入错误，只许输入'F'或'M',请重新输入";
			cin >> astReader[ReaderID].Sex;
			cout << endl;
		}
		break;
	case 4:
		Borrow = astReader[ReaderID].Max - astReader[ReaderID].Amount;
		cout << endl << "\t请输入读者最大可借阅的数量(范围为20以内)：";
		cin >> Max;
		cout << endl;

		while (Max != -1)//bug输入-1会退出
		{
			if (Max >= 0 && Max <= 20)
			{
				if (Borrow > Max)
				{
					cout << "\t请先还书!" << endl;
					return;
				}
				else
				{
					astReader[ReaderID].Max = Max;
					astReader[ReaderID].Amount = Max - Borrow;
					for (int i = Borrow; i < Max; i++)
					{
						astReader[ReaderID].BookId[i] = 0;
					}
					break;
				}
			}
			cout << "\t错误,读者最大可借阅的书的范围为20以内,请重新输入：";
			cin >> Max;
			cout << endl;
		}
		break;
	}
	//保存到文件中
	SaveReaderFile(ReaderID);
	cout << "\t读者信息修改成功";
}

//读者管理模块
//读者已借阅的数量=Max-Amount
void MangaeReader()
{
	int item;
	ShowReaderMenu();
	cin >> item;
	while (item)
	{
		switch (item)
		{
		case 1:
			ShowReader();//显示读者信息
			break;
		case 2:
			AddReader();//添加读者信息
			break;
		case 3:
			SearchReader();//查找图读者信息
			break;
		case 4:
			DeleteReader();//删除读者信息
			break;
		case 5:
			ModifyReader();//修改读者信息
			break;
		default:
			cout << "\t\t" << "输入错误，请重新输入" << endl;
			Sleep(1500);
			break;
		}
		cout << endl << "按任意键返回" << endl;
		_getch();//从控件中获取字符而无需显示
		ShowReaderMenu();
		cin >> item;
	}
}

//借书模块
void BorrowBook()
{
	//读者是否可以借书
	int BookID, ReaderID;
	int Borrow;//读者当前借阅的书数

	system("cls");
	//输入要借书的读者编号，判断编号是否存在，如果再显示读者的借阅的图书信息
	ReaderID = SearchReader();
	cout << endl;
	if (ReaderID == -1)
	{
		return;
	}
	if (ReaderID == -3)
	{
		return;
	}


	Borrow = astReader[ReaderID].Max - astReader[ReaderID].Amount;
	if (Borrow == 0)
	{
		cout << "\t该读者没有借阅任何书" << endl;
	}
	else
	{
		cout << "\t该读者借阅的图书为：" << endl;
		for (int i = 0; i < Borrow; i++)
		{
			cout << "\t" << astReader[ReaderID].BookId[i] << endl;
		}
	}
	if (astReader[ReaderID].Amount == 0)
	{
		cout << "\t该读者可借阅书数目为0，不能继续借书" << endl;
	}


	//图书是否可借阅
	cout << "\t按任意键进入选择借阅图书环节：";
	_getch();
	BookID = SearchBook();
	if (BookID == -1)
	{
		return;
	}
	if (BookID == -3)
	{
		return;
	}
	if (astBook[BookID].Amount == 0)
	{
		cout << "\t该图书库存量为0，图书不可借阅" << endl;
	}

	//借书
	astReader[ReaderID].BookId[Borrow] = astBook[BookID].Num;
	//图书库存-1
	astBook[BookID].Amount--;
	//当前读者可借阅数量-1
	astReader[ReaderID].Amount--;
	SaveBookFile(BookID);
	SaveReaderFile(ReaderID);
	
	cout << "\t借书成功";
}

//还书模块
void ReturnBook()
{
	int BookID, ReaderID;
	int Borrow;//读者当前借阅的书数

	system("cls");
	//读者
	ReaderID = SearchReader();
	cout << endl;
	if (ReaderID == -1)
	{
		return;
	}
	if (ReaderID == -3)
	{
		return;
	}

	Borrow = astReader[ReaderID].Max - astReader[ReaderID].Amount;
	if (Borrow == 0)
	{
		cout << "\t该读者没有借阅任何书,无需归还" << endl;
	}
	else
	{
		cout << "\t该读者借阅的图书为：" << endl;
		for (int i = 0; i < Borrow; i++)
		{
			cout << "\t" << astReader[ReaderID].BookId[i] << endl;
		}
	}
	if (astReader[ReaderID].Amount == 0)
	{
		cout << "\t该读者可借阅书数目为0，不能继续借书" << endl;
	}


	//图书
	cout << "\t按任意键进入选择归还图书环节：";
	_getch();
	BookID = SearchBook();
	if (BookID == -1)
	{
		return;
	}
	if (BookID == -3)
	{
		return;
	}

	int i;
	for (i = 0; i < Borrow; i++)
	{
		if (astReader[ReaderID].BookId[i] == astBook[BookID].Num)
		{
			//还书
			for (int j = i; j < Borrow - 1; j++)
			{
				astReader[ReaderID].BookId[j] = astReader[ReaderID].BookId[j + 1];
			}
			astReader[ReaderID].BookId[Borrow - 1] = 0;//清除最后一个信息

			//图书库存+1
			astBook[BookID].Amount++;
			//读者可借阅数量+1
			astReader[ReaderID].Amount++;
			break;
		}
	}

	//如果借书列表里面没有这本书
	if (i == Borrow)
	{
		cout << "\t该读者没有借阅这本书，无需归还";
		return;
	}

	SaveBookFile(BookID);
	SaveReaderFile(ReaderID);
	cout << "\t还书成功";

}

//借还书模块
void BorrowReturnManger()
{
	ShowBorrowReturnMenu();
	int item;
	cin >> item;

	while (item)
	{
		switch (item)
		{
		case 1:
			BorrowBook();//借书模块
			break;
		case 2:
			ReturnBook();//还书模块
			break;
		default:
			cout << "\t\t" << "输入错误，请重新输入" << endl;
			Sleep(1500);
			break;
		}
		cout << endl << "按任意键返回" << endl;
		_getch();//从控件中获取字符而无需显示
		ShowBorrowReturnMenu();
		cin >> item;
	}
}

int main()
{
	Menu();
	int item;
	cin >> item;
	while (item)
	{
		switch (item)
		{
		case 1:
			MangaeBook();//图书管理模块
			break;
		case 2:
			MangaeReader();//读者管理模块
			break;
		case 3:
			BorrowReturnManger();//借还书模块
			break;
		default:
			cout << "\t\t" << "输入错误，请重新输入" << endl;
			Sleep(1500);
			break;
		}
		Menu();
		cin >> item;

	}
	return 0;
}


